 //
//  SetUpPasswordViewController.m
//  PigMarket
//
//  Created by mac on 2019/4/15.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "SetUpPasswordViewController.h"
#import "LoginViewController.h"

@interface SetUpPasswordViewController ()

//setUpPasswordlab
@property (nonatomic, strong) UILabel *setUpPasswordlab;

//passwordield
@property (nonatomic, strong) UITextField *passwordField;

//下划线
@property (nonatomic, strong) UIView *line1;

//confirmPasswordField
@property (nonatomic, strong) UITextField *confirmPasswordField;

//下划线
@property (nonatomic, strong) UIView *line2;
/**
 confirmPasswordSecutityBtn
 */
@property (nonatomic , strong) UIButton *passwordSecurityBtn;

/**
 confirmPasswordSecutityBtn
 */
@property (nonatomic , strong) UIButton *confirmPasswordSecutityBtn;

//confirmbtn
@property (nonatomic, strong) UIButton *confirmBtn;


@property (nonatomic, strong) UILabel *tipLab;


@end

@implementation SetUpPasswordViewController


#pragma mark - view life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = KWhiteColor;
    
    [self setUpView];
}


#pragma mark - init method

- (void)setUpView {
    
    [self initView];
    [self initLayout];
    [self initAction];
    [self initData];
}


- (void)initView {
    
    [self.view addSubview:self.setUpPasswordlab];
    [self.view addSubview:self.passwordSecurityBtn];
    [self.view addSubview:self.passwordField];
    [self.view addSubview:self.line1];
    [self.view addSubview:self.confirmPasswordSecutityBtn];
    [self.view addSubview:self.confirmPasswordField];
    [self.view addSubview:self.line2];
    [self.view addSubview:self.confirmBtn];
    [self.view addSubview:self.tipLab];

}


- (void) initLayout {
    
    [self.setUpPasswordlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.top.mas_equalTo(36+TJ_NavBarHeight);
        make.height.mas_equalTo(30);
    }];
    
    
    [self.passwordSecurityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.setUpPasswordlab.mas_bottom).offset(55);
        make.right.mas_equalTo(-30);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    
    
    [self.passwordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.centerY.mas_equalTo(self.passwordSecurityBtn.mas_centerY);
        make.right.mas_equalTo(self.passwordSecurityBtn.mas_left).offset(-10);
        make.height.mas_equalTo(22);
    }];
    
    
    [self.line1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(1);
        make.top.mas_equalTo(self.passwordField.mas_bottom).offset(16);
    }];
    [self.confirmPasswordSecutityBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.line1.mas_bottom).offset(30);
        make.right.mas_equalTo(-30);
        make.size.mas_equalTo(CGSizeMake(20, 20));
    }];
    [self.confirmPasswordField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.centerY.mas_equalTo(self.confirmPasswordSecutityBtn.mas_centerY);
        make.right.mas_equalTo(self.confirmPasswordSecutityBtn.mas_left).offset(-10);
        make.height.mas_equalTo(22);
    }];
    [self.line2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.confirmPasswordField.mas_bottom).offset(16);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.height.mas_equalTo(1);
    }];
    
    [self.confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(self.line2.mas_bottom).offset(40);
        make.height.mas_equalTo(48);
    }];
    
    [self.tipLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
        make.top.mas_equalTo(self.confirmBtn.mas_bottom).offset(25);
        make.height.mas_equalTo(40);
    }];

}

- (void) initAction {
    
    [self.passwordSecurityBtn addTarget:self action:@selector(passwordSecurityTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmPasswordSecutityBtn addTarget:self action:@selector(confirmPasswordSecurityTapped) forControlEvents:UIControlEventTouchUpInside];
    [self.confirmBtn addTarget:self action:@selector(confirmBtnTapped) forControlEvents:UIControlEventTouchUpInside];
}

- (void)initData {

}

#pragma mark - delegate

#pragma mark - target action
- (void)passwordSecurityTapped {
    self.passwordField.secureTextEntry = !self.passwordField.secureTextEntry;
    self.passwordSecurityBtn.selected = !self.passwordSecurityBtn.selected;
}
- (void)confirmPasswordSecurityTapped {
    self.confirmPasswordField.secureTextEntry = !self.confirmPasswordField.secureTextEntry;
    self.confirmPasswordSecutityBtn.selected = !self.confirmPasswordSecutityBtn.selected;
}
- (void)confirmBtnTapped {
    
    [self modifyPasswordRequest];
}


#pragma mark - private method

- (void)modifyPasswordRequest {
    
    NSString *password = self.passwordField.text;
    NSString *confirmPassword = self.confirmPasswordField.text;

    if ([password isBlankString] || [confirmPassword isBlankString] || ![password isValidPassword] || ![confirmPassword isValidPassword]) {
        [self toastWithContent:@"请输入6-16位新密码"];
        return;
    }else if (![password isEqualToString:confirmPassword]) {
        [self toastWithContent:@"两次密码输入不一致"];
        return;
    }

    @weakify(self);
    [[[PigMarketApi sharedInstance] userSetUpPasswordWithAccount:self.phone password:password] subscribeNext:^(NSNull * _Nullable x) {
        @strongify(self);

        [self.navigationController.childViewControllers enumerateObjectsUsingBlock:^(__kindof UIViewController * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            if ([obj isKindOfClass:[LoginViewController class]]) {
                [self.navigationController popToViewController:obj animated:YES];
            }
        }];
        
    } error:^(NSError * _Nullable error) {

    }];
    
}
#pragma mark - getter and setter
- (UILabel *)setUpPasswordlab {
    if (!_setUpPasswordlab) {
        _setUpPasswordlab = [[UILabel alloc] init];
        _setUpPasswordlab.textColor = [UIColor textBlackColor];
        _setUpPasswordlab.font = mFont(22);
        _setUpPasswordlab.text = @"设置密码";
    }
    return _setUpPasswordlab;
}


- (UITextField *)passwordField {
    if (!_passwordField) {
        _passwordField = [[UITextField alloc] init];
        _passwordField.placeholder = @"请输入6-16位新密码";
        _passwordField.font = mFont(16);
        _passwordField.secureTextEntry = YES;
    }
    return _passwordField;
}


- (UITextField *)confirmPasswordField {
    if (!_confirmPasswordField) {
        _confirmPasswordField = [[UITextField alloc] init];
        _confirmPasswordField.placeholder = @"请再次输入密码";
        _confirmPasswordField.font = mFont(16);
        _confirmPasswordField.secureTextEntry = YES;
    }
    return _confirmPasswordField;
}


- (UIButton *)passwordSecurityBtn {
    if (!_passwordSecurityBtn) {
        _passwordSecurityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_passwordSecurityBtn setImage:[UIImage imageNamed:@"login_hidden"] forState:UIControlStateNormal];
        [_passwordSecurityBtn setImage:[UIImage imageNamed:@"login_show"] forState:UIControlStateSelected];
    }
    return _passwordSecurityBtn;
}


- (UIButton *)confirmPasswordSecutityBtn {
    if (!_confirmPasswordSecutityBtn) {
        _confirmPasswordSecutityBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmPasswordSecutityBtn setImage:[UIImage imageNamed:@"login_show"] forState:UIControlStateSelected];
        [_confirmPasswordSecutityBtn setImage:[UIImage imageNamed:@"login_hidden"] forState:UIControlStateNormal];
        
    }
    return _confirmPasswordSecutityBtn;
}


- (UIButton *)confirmBtn {
    if (!_confirmBtn) {
        _confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_confirmBtn setTitle:@"确定" forState:UIControlStateNormal];
        _confirmBtn.titleLabel.font = mFont(16);
        [_confirmBtn setBackgroundImage:[UIImage imageNamed:@"login_loginbtn_default"] forState:UIControlStateNormal];
    }
    return _confirmBtn;
}


- (UIView *)line1 {
    if (!_line1) {
        _line1 = [[UIView alloc] init];
        _line1.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.08];
    }
    return _line1;
}


- (UIView *)line2 {
    if (!_line2) {
        _line2 = [[UIView alloc] init];
        _line2.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.08];
    }
    return _line2;
}


- (UILabel *)tipLab{
    
    if (!_tipLab) {
        _tipLab = [UILabel makeLabelWithText:@"密码必须是6-16位的数字、字母组合(不能是纯数字)" textColor:UIColorFromRGB(0x7A7A7A) font:mFont(14) textAliment:0];
        _tipLab.numberOfLines = 0;
    }
    return _tipLab;
}

@end
